﻿/*-------------------------------------------------------------------------
* 命名空间: MyToDo.ViewModels/MainViewModel
* 类       名: MainViewModel
* 功      能:  TD
* 时      间:  2023/12/12 16:51:48
* 版      本:  V1.0
* 作      者:  Fioman
* 格      言:  Talk is cheap,show me the code ^_^
*-------------------------------------------------------------------------*/
using MyToDo.Extensions;
using MyToDo.Models;
using MyToDo.Views;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.ObjectModel;
using System.Diagnostics;

namespace MyToDo.ViewModels
{
    public class MainViewModel : BindableBase
    {

        public MainViewModel(IRegionManager RegionManager)
        {
            _menuBars = new ObservableCollection<MenuBar>();
            InitMenuBars();

            NavigateCommand = new DelegateCommand<MenuBar>(DoNavigateCommand);
            _regionManager = RegionManager;

            GoBackCommand = new DelegateCommand(DoGoBackCommand);
            GoForwardCommand = new DelegateCommand(DoGoForwardCommand);

            // 不能在这里来设置主页要展示的默认页面,因为contentRegion还没进行初始化,在这里.
            // 要在MainView的Load事件函数中去加载和设置主页要现实的默认页面
            //_regionManager.Regions[PrismManager.MainViewRegion].RequestNavigate(nameof(IndexView));
        }

        private void DoGoForwardCommand()
        {
            if (NavJournal != null && NavJournal.CanGoForward)
                NavJournal.GoForward();
        }

        private void DoGoBackCommand()
        {
            if (NavJournal != null && NavJournal.CanGoBack)
                NavJournal.GoBack();
        }

        private void DoNavigateCommand(MenuBar MenuBarObj)
        {
            if (MenuBarObj != null && !string.IsNullOrWhiteSpace(MenuBarObj.ViewName))
            {
                _regionManager.Regions[PrismManager.MainViewRegion].RequestNavigate(MenuBarObj.ViewName, NavCallback);
            }
        }

        /// <summary>
        /// 导航函数执行完之后的回调函数,主要用来添加操作日志这里
        /// </summary>
        /// <param name="NavResult"></param>
        private void NavCallback(NavigationResult NavResult)
        {
            if ((bool)NavResult.Result!)
            {
                NavJournal = NavResult.Context.NavigationService.Journal;
            }
        }

        public DelegateCommand GoBackCommand { get; private set; }

        public DelegateCommand GoForwardCommand { get; private set; }

        /// <summary>
        /// 导航日志,用来进行上一步和下一步的操作
        /// </summary>
        private IRegionNavigationJournal? NavJournal { get; set; }

        /// <summary>
        /// 导航栏操作命令
        /// </summary>
        public DelegateCommand<MenuBar> NavigateCommand { get; private set; }

        private readonly IRegionManager _regionManager;

        /// <summary>
        /// 初始化左侧的抽屉式导航栏内容
        /// </summary>
        private void InitMenuBars()
        {
            MenuBars.Add(new MenuBar() { Icon = "Home", Title = "首页", ViewName = "IndexView" });
            MenuBars.Add(new MenuBar() { Icon = "NotebookEditOutline", Title = "待办事项", ViewName = "ToDoView" });
            MenuBars.Add(new MenuBar() { Icon = "NotebookPlusOutline", Title = "备忘录", ViewName = "MemoView" });
            MenuBars.Add(new MenuBar() { Icon = "Cog", Title = "设置", ViewName = "SettingsView" });
        }





        private ObservableCollection<MenuBar> _menuBars;

        public ObservableCollection<MenuBar> MenuBars
        {
            get { return _menuBars; }
            set
            {
                _menuBars = value;
                RaisePropertyChanged();
            }
        }



        /// <summary>
        /// 应用程序最大化和最小化的状态,主要用来鼠标悬浮的时候来提示文字用.
        /// </summary>
        private string? appMaxmizeState;

        public string? AppMaxmizeState
        {
            get { return appMaxmizeState; }
            set
            {
                appMaxmizeState = value;
                RaisePropertyChanged();
            }
        }
    }
}
